
import { readFileSync , watch } from 'fs';

interface UserInterface {
    username: string
    password: string
    authentication: string
}

class Authentication  {
    private user: UserInterface ;
    constructor(){
        this.user = JSON.parse(readFileSync('config.json', 'utf8'));
    }
    upUser(){
        this.user = JSON.parse(readFileSync('config.json', 'utf8'));
    }

    checkPath(path: string){
        return path.substring(1) === this.user.authentication;
    }

    check(user: any){
        if(user === null){
            return false;
        }
        try{
            return this.user.username === user.username && this.user.password === user.password;
        }catch{
            return false;
        }
    }
}
var authentication = new Authentication();
watch('config.json', (event: string, filename: string) => {
    if (event === 'change'){
        console.log("change" + filename)
        try {
            authentication.upUser();
        } catch (error) {
            console.log(error)
        }
        
    }
})

export default authentication;